package com.cmy.framework_widget.wheelview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect

class WheelSelect(//黑框的Y坐标起点、宽度、高度
    var startY: Int,
    private val width: Int,
    private val height: Int, //需要选择文本的颜色、大小、补白
    private val selectText: String?,
    private val fontColor: Int,
    private val fontSize: Int,
    private val padding: Int
) {
    //四点坐标
    private val rect = Rect()
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val mPaintLine = Paint()

    init {
        rect.left = 0
        rect.top = startY
        rect.right = width
        rect.bottom = startY + height
        mPaintLine.strokeWidth = 4f
        mPaintLine.color = Color.parseColor("#EEEEEE")
    }

    fun onDraw(mCanvas: Canvas) {
        //绘制背景
        mPaint.style = Paint.Style.FILL
        mPaint.color = COLOR_BACKGROUND
        mCanvas.drawRect(rect, mPaint)
        //绘制提醒文字
        if (selectText != null) {
            //设置钢笔属性
            mPaint.textSize = fontSize.toFloat()
            mPaint.color = fontColor
            //得到字体的宽度
            val textWidth = mPaint.measureText(selectText).toInt()
            //drawText的绘制起点是左下角,y轴起点为baseLine
            val metrics = mPaint.fontMetrics
            val baseLine =
                (rect.centerY() + (metrics.bottom - metrics.top) / 2 - metrics.bottom).toInt()
            //在靠右边绘制文本
            mCanvas.drawText(
                selectText,
                (rect.right - padding - textWidth).toFloat(),
                baseLine.toFloat(),
                mPaint
            )
        }
        mCanvas.drawLine(
            100f,
            rect.top.toFloat(),
            (width - 100).toFloat(),
            rect.top.toFloat(),
            mPaintLine
        )
        mCanvas.drawLine(
            100f,
            rect.bottom.toFloat(),
            (width - 100).toFloat(),
            rect.bottom.toFloat(),
            mPaintLine
        )
    }

    companion object {
        //黑框背景颜色
        val COLOR_BACKGROUND = Color.parseColor("#00000000")
    }
}